package com.alks.function.service.impl.stockdept.pcfactoryquery;

import com.alks.common.aop.AutoPageAop;
import com.alks.common.data.thread.UserIdThread;
import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.common.utils.beanutils.StringUtils;
import com.alks.entity.data.entity.PcSurplusAcc;
import com.alks.entity.data.entity.PcSurplusDetail;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.dto.chengKong.assort.SdMtlTypeNewDto;
import com.alks.function.data.dto.stockdept.pcfactoryquery.SurplusStkDetDto;
import com.alks.function.data.request.stockdept.pcfactoryquery.SurplusStkRequest;
import com.alks.function.mapper.stockdept.pcfactorystockquery.PcSurplusAccMapper;
import com.alks.function.mapper.stockdept.pcfactorystockquery.SD_SQ_PcSurplusDetailMapper;
import com.alks.function.service.stockdept.pcfactoryquery.ISurplusStkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author MS
 * @since 2024/8/12下午3:45
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class SurplusStkServiceImpl extends ServiceImpl<PcSurplusAccMapper, PcSurplusAcc> implements ISurplusStkService {
    private final SD_SQ_PcSurplusDetailMapper detailMapper;
    private final PcSurplusAccMapper accMapper;

    @Override
    @AutoPageAop
    public ResponseInfo temp(SurplusStkRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        String space = " ";
        /*排序字段处理*/
        if (request.getOrderFile().contains(space)) {
            return ResponseInfo.error("排序字段不能包含空格");
        }
        request.setOrderFile(StringUtils.convertToUpper(request.getOrderFile()));
        /*查询数据*/
        List<PcSurplusAcc> dtos = accMapper.tempQuery(request, companyId);
        Page<PcSurplusAcc> page = (Page<PcSurplusAcc>) dtos;
        Map<String, BigDecimal> sum = accMapper.tempSum(request, companyId);
        /*封装返回*/
        Map<String, Object> map = new HashMap<>(3);
        map.put("dto", dtos);
        map.put("sum", sum);
        map.put("total", page.getTotal());
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo detail(String mtlNo, String slipType) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        LambdaQueryWrapper<PcSurplusDetail> wrapper = new LambdaQueryWrapper<PcSurplusDetail>()
                .eq(PcSurplusDetail::getMtlNo, mtlNo)
                .eq(PcSurplusDetail::getSlipType, slipType)
                .eq(PcSurplusDetail::getCompanyId, companyId);
        List<PcSurplusDetail> details = detailMapper.selectList(wrapper);
        /*封装返回*/
        List<SurplusStkDetDto> dtos = BeanUtil.copyList(details, SurplusStkDetDto.class);
        BigDecimal sum = BigDecimal.ZERO;
        if (details != null) {
            sum = details.stream()
                    .filter(Objects::nonNull)
                    .map(PcSurplusDetail::getQty)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
        }
        Map<String, Object> map = new HashMap<>(2);
        map.put("dto", dtos);
        map.put("sum", sum);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo back(SurplusStkRequest request) {
        String companyId = UserIdThread.get().getCompanyId();
        /*获取数据*/
        List<SdMtlTypeNewDto> slipTypeList = accMapper.surplusStkBack(request, companyId);
        List<Map<String, String>> slipType = slipTypeList.stream()
                .map(a -> {
                    Map<String, String> map = new HashMap<>(2);
                    map.put("typeNo",a.getTypeNo());
                    map.put("typeName",a.getTypeName());
                    return map;
                }).collect(Collectors.toList());
        return ResponseInfo.ok(slipType);
    }
}
